package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "RegisterActivity";
    private Context mContext;
    private EditText register_username_et;
    private EditText register_password_et;
    private Button register_logging_btn;
    private TextView register_jp_login_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mContext = this;

        initView();
    }

    private void initView() {

        register_username_et = findViewById(R.id.register_username_et);
        register_password_et = findViewById(R.id.register_password_et);
        register_logging_btn = findViewById(R.id.register_logging_btn);
        register_logging_btn.setOnClickListener(this);
        register_jp_login_tv = findViewById(R.id.register_jp_login_tv);
        register_jp_login_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_logging_btn:
                String username = register_username_et.getText().toString().trim();
                String password  = register_password_et.getText().toString().trim();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(mContext,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (username.length() < 4){
                    Toast.makeText(mContext,"用户名长度不能小于4",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.length() < 4){
                    Toast.makeText(mContext,"密码长度不能小于4",Toast.LENGTH_SHORT).show();
                    return;
                }

                //注册
                JMessageClient.register(username, password, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        Log.i(TAG,"注册：返回编号:"+responseCode+" ,返回数据:"+responseMessage);
                        if (responseCode == 0 && responseMessage.equals("Success")){
                            Toast.makeText(mContext,"注册成功",Toast.LENGTH_SHORT).show();
                        }else if (responseCode == 898001 && responseMessage.equals("user exist")){
                            Toast.makeText(mContext,"用户已存在",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(mContext,"注册失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

/**
 * 注册同时指定用户信息中的其他字段
 * @since 2.3.0
 */
                 //RegisterOptionalUserInfo 注册时的用户其他信息
                //JMessageClient.register(String userName, String password, RegisterOptionalUserInfo optionalUserInfo, BasicCallback callback);

                break;
            case R.id.register_jp_login_tv:
                finish();
                break;
        }
    }
}
